﻿using System;
using System.Collections.Generic;
using System.Text;
using QLPMT_DTO;
using System.Data.SqlClient;
namespace QLPMT_DAO
{
   public class KhamBenhDAO
    {
            DataProvider provider;
         public  KhamBenhDAO()
           {
               provider = new DataProvider();
           }
       //danh sach kham benh
       public List<KhamBenh> DanhSachKhamBenh()
       {
           provider.Connect();
           string sql = "select * from khambenh";
          SqlDataReader reader = provider.ExecuteReader(sql);
           List<KhamBenh> list = new List<KhamBenh>();
           while (reader.Read())
           {
               string makhambenh = reader.GetString(0);
               string mabenhnhan = reader.GetString(1);
               DateTime ngaykham = reader.GetDateTime(2);
               KhamBenh khambenh = new KhamBenh(makhambenh, mabenhnhan, ngaykham);
               list.Add(khambenh);
           }
           provider.Disconnect();
           return list;
       }
           //lay thong tin kham benh
           public KhamBenh GetThongTinKhamBenh(string ma)
           {
               provider.Connect();
               string sql = "select * from khambenh where makhambenh="+ma;
               SqlDataReader reader = provider.ExecuteReader(sql);
                   string makhambenh = reader.GetString(0);
                   string mabenhnhan = reader.GetString(1);
                   DateTime ngaykham=reader.GetDateTime(2);
                   KhamBenh khambenh = new KhamBenh(makhambenh,mabenhnhan,ngaykham);
               provider.Disconnect();
               return khambenh ;
           }
      
       //lay thong tin kham benh theo ngay kham
       public List<KhamBenh> GetThongTinKhamBenhTheoNgayKham(DateTime dtngaykham)
       {
           provider.Connect();
           string sql = "select * from khambenh where datediff(dd,NgayKham,'"+dtngaykham+"')=0";
           SqlDataReader reader = provider.ExecuteReader(sql);
           List<KhamBenh> list = new List<KhamBenh>();
           while (reader.Read())
           {
               string makhambenh = reader.GetString(0);
               string mabenhnhan = reader.GetString(1);
               DateTime ngaykham = reader.GetDateTime(2);
               KhamBenh khambenh = new KhamBenh(makhambenh, mabenhnhan, ngaykham);
               list.Add(khambenh);
           }
           
           provider.Disconnect();
           return list;
       }
       // Them benh nhan vao danh sach kham benh
       public void ThembenhNhanVaoDanhSachKhamBenh(KhamBenh KB)
       {
           provider.Connect();
           string sql = "insert into KhamBenh(MaKhamBenh,MaBenhNhan,NgayKham) values('"+KB.Makhambenh+"','"+KB.Mabenhnhan+"','"+KB.Ngaykham+"')";
           provider.ExecuteQuery(sql);
           provider.Disconnect();
       }
       // Lay ra ngay kham da thanh toan hoa don
       public List<DateTime> DanhSachNgayKham(int thang)
       {
           provider.Connect();
           DateTime ngay = DateTime.Now;
           string sql = "select KhamBenh.* from KhamBenh,PhieuKham,HoaDon where KhamBenh.MaKhamBenh=PhieuKham.MaKhamBenh and PhieuKham.MaPhieuKham=HoaDon.MaPhieuKham and DATEPART(mm,NgayKham)="+thang+" and datediff(dd,NgayKham,'"+ngay+"')<365";
           SqlDataReader reader = provider.ExecuteReader(sql);
           List<DateTime> list = new List<DateTime>();
           while (reader.Read())
           {
               DateTime temp = reader.GetDateTime(2);
               list.Add(temp);
           }
           provider.Disconnect();
           return list;
       }
    }
}
